Skip to content

Add withoutDetection() method for suppressing N+1 detection#116

Open
dingyaguang117 wants to merge 1 commit intobeyondcode:masterfrom
dingyaguang117:feature/add-without-detection
Open

Add withoutDetection() method for suppressing N+1 detection#116
dingyaguang117 wants to merge 1 commit intobeyondcode:masterfrom
dingyaguang117:feature/add-without-detection

Conversation

@dingyaguang117
Copy link

Motivation

The existing except config requires whitelisting by model + relation pair, which is too coarse-grained. In practice, developers often need to suppress detection for a specific code block (e.g. admin pages with small datasets, legacy endpoints, or background jobs) rather than globally whitelisting a relation.

What I did

  • Add QueryDetector::withoutDetection(callable $callback) method that temporarily disables N+1 detection for the duration of the closure
  • Uses a simple $disabled flag — no reflection or backtrace scanning overhead
  • The closure's return value is passed through
  • Detection resumes automatically after the closure, even if it throws an exception

Usage

  app(QueryDetector::class)->withoutDetection(function () {
      // N+1 queries here will not be reported
      foreach (Author::all() as $author) {
          $author->posts;
      }
  });

@dingyaguang117
Copy link
Author

Hi @mpociot @mechelon,

This PR is ready for review. When you have a chance, could you please take a look and let me know if you’d like any changes? If it looks good to you, I’d really appreciate a merge.

I’ve tried to keep the change minimal and aligned with the existing behavior/style, and I’m happy to adjust or add tests/docs if needed.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant